package com.innovations.inn.service.impl;

import com.innovations.common.utils.DateUtils;
import com.innovations.common.utils.uuid.IdGeneratorSnowflake;
import com.innovations.inn.domain.ResearchAccountApply;
import com.innovations.inn.mapper.ResearchAccountApplyMapper;
import com.innovations.inn.service.IResearchAccountApplyService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 科研账号申请Service业务层处理
 *
 * @author dp
 * @date 2024-08-23
 */
@Service
public class ResearchAccountApplyServiceImpl implements IResearchAccountApplyService {
    @Autowired
    ResearchAccountApplyMapper researchAccountApplyMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;

    /**
     * 查询科研账号申请
     *
     * @param applyId 科研账号申请主键
     * @return 科研账号申请
     */
    @Override
    public ResearchAccountApply selectResearchAccountApplyByApplyId(Long applyId) {
        return researchAccountApplyMapper.selectResearchAccountApplyByApplyId(applyId);
    }

    /**
     * 查询科研账号申请列表
     *
     * @param researchAccountApply 科研账号申请
     * @return 科研账号申请
     */
    @Override
    public List<ResearchAccountApply> selectResearchAccountApplyList(ResearchAccountApply researchAccountApply) {
        return researchAccountApplyMapper.selectResearchAccountApplyList(researchAccountApply);
    }

    /**
     * 新增科研账号申请
     *
     * @param researchAccountApply 科研账号申请
     * @return 结果
     */
    @Override
    public int insertResearchAccountApply(ResearchAccountApply researchAccountApply) {
        return researchAccountApplyMapper.insertResearchAccountApply(researchAccountApply);
    }

    /**
     * 修改科研账号申请
     *
     * @param researchAccountApply 科研账号申请
     * @return 结果
     */
    @Override
    public int updateResearchAccountApply(ResearchAccountApply researchAccountApply) {
        return researchAccountApplyMapper.updateResearchAccountApply(researchAccountApply);
    }

    /**
     * 批量删除科研账号申请
     *
     * @param applyIds 需要删除的科研账号申请主键
     * @return 结果
     */
    @Override
    public int deleteResearchAccountApplyByApplyIds(Long[] applyIds) {
        return researchAccountApplyMapper.deleteResearchAccountApplyByApplyIds(applyIds);
    }

    /**
     * 删除科研账号申请信息
     *
     * @param applyId 科研账号申请主键
     * @return 结果
     */
    @Override
    public int deleteResearchAccountApplyByApplyId(Long applyId) {
        return researchAccountApplyMapper.deleteResearchAccountApplyByApplyId(applyId);
    }

    /**
     * 实体数据补全
     *
     * @param researchAccountApply 实体
     * @param userId               用户id
     * @param crateType            创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(ResearchAccountApply researchAccountApply, Long userId, Integer crateType) {
        // 标识
        if (researchAccountApply.getApplyId() == null || "".equals(researchAccountApply.getApplyId())) {
            researchAccountApply.setApplyId(idGeneratorSnowflake.nextId());
        }

        // 创建人、创建时间
        if (crateType == 1) {
            if (researchAccountApply.getCreateBy() == null || "".equals(researchAccountApply.getCreateBy())) {
                researchAccountApply.setCreateBy(userId);
            }
            if (researchAccountApply.getCreateTime() == null) {
                researchAccountApply.setCreateTime(DateUtils.getNowDate());
            }
        }


        // 逻辑删除
        if (researchAccountApply.getIsDeleted() == null || "".equals(researchAccountApply.getIsDeleted())) {
            researchAccountApply.setIsDeleted(0L);
        }
    }

}
